package 第一个程序;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Scanner;

public class UDPServer {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        DatagramSocket ds = null; //套接字
        DatagramPacket dp = null; // 数据包
        {
        try{
            ds = new DatagramSocket(10000);
            byte[] by = new byte[1024];
            dp = new DatagramPacket(by,by.length);
            ds.receive(dp);
            String str = new String(dp.getData(),0,dp.getLength());

            System.out.println("请输入用户名：");
            String str1 = in.nextLine();
            String str2 = dp.getAddress().toString();
            if(str1.equals(str2)){
                System.out.println("欢迎光临");
            }
            else{
                System.out.println("用户不存在");
            }

        //    System.out.println(str + "--" + dp.getAddress());
            }
        catch (SocketException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(ds != null )
                ds.close();
        }
        }
    }
}
